package hhf.common.acid.operation {
	import mx.messaging.messages.RemotingMessage;
	import mx.rpc.AbstractOperation;
	import mx.rpc.AbstractService;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	
	import hhf.common.acid.service.BeanService;
	import mx.core.mx_internal;

	use namespace mx_internal;

	public class Operation extends AbstractOperation {
		final public function Operation(service:AbstractService=null, name:String=null) {
			this.mx_internal::_service = service;
			this.name = name;
			this.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void {
				(event.target as AbstractOperation).setResult(event.result);
			}, false);
		}
    	override public function send(... args:Array):AsyncToken {
	        var message:RemotingMessage = new RemotingMessage();
	        message.operation = name;
	        message.body = args;
	        message.source = ""+this.service;

    		var token:AsyncToken = null;
    		var serv:BeanService = this.service as BeanService;
    		token = serv.asyncTokens[name+"AsyncToken"];
    		if(token!=null) {
    			token.operation = this;
	    		token.mx_internal::setMessage(message);
	    		token.execute.apply(token, args); 
	    	}
        	return token;
    	}
	}
}